//1658. 将 x 减到 0 的最小操作数

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        int ret = -1,sum = 0;
        for(int i = 0; i < nums.size(); i++)
        {   
            sum += nums[i];
        }
        int target = sum - x;
        if(target < 0) return -1;
        sum = 0;
        for(int left = 0, right = 0;right < nums.size(); right++)
        {
            sum += nums[right];
            while(sum > target)
            {
                sum -= nums[left++];
            }
            if(sum == target)
            {
                ret = max(ret, right - left + 1);
            }
        }
        if(ret == -1) return ret;
        else return nums.size() - ret;
    }
};